package proto

import (
	"fmt"
	"regexp"
	"sort"
	"strings"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
)

/*
[DEBUG] [2024/10/04 10:33:26] [PRJ/websocket_apn/main_test.go]:[  193]:   ↑↑↑↑↑ S_DRAGON_HOME_EXPLORE_DATA - len:23
[DEBUG] [2024/10/04 10:33:26] [PRJ/websocket_apn/main_test.go]:[  200]:   PayloadBytes: 71 AB 00 00 00 17 00 03 37 84 00 00 02D4 07 01 81 E2 08 B3 E7 E7 04
[DEBUG] [2024/10/04 10:33:26] [PRJ/websocket_apn/main_test.go]:[  195]:   ↓↓↓↓↓ game cmd[10820]-unkown - len:847
[DEBUG] [2024/10/04 10:33:26] [PRJ/websocket_apn/main_test.go]:[  200]:   PayloadBytes: 71 AB 00 00 03 4F 00 00 2A 44 00 00 02D4 07 01 81 E2 08 00 12 B8 06 08 01 12 08 0A 04 08 00 10 00 10 00 12 08 0A 04 08 00 10 01 10 00 12 08 0A 04 08 01 10 00 10 00 12 08 0A 04 08 00 10 02 10 00 12 08 0A 04 08 01 10 01 10 07 12 08 0A 04 08 02 10 00 10 00 12 08 0A 04 08 00 10 0310 00 12 08 0A 04 08 01 10 02 10 00 12 08 0A 04 08 02 10 01 10 00 12 08 0A 04 08 03 10 00 10 00 12 08 0A 04 08 00 10 04 10 00 12 08 0A 04 08 01 10 03 10 00 12 08 0A 04 08 02 10 02 10 00 12 08 0A 04 08 03 10 01 10 00 12 08 0A 04 08 04 10 0010 00 12 08 0A 04 08 00 10 05 10 00 12 08 0A 04 08 01 10 04 10 07 12 08 0A 04 08 02 10 03 10 04 12 08 0A 04 08 03 10 02 10 00 12 08 0A 04 08 04 10 01 10 07 12 08 0A 04 08 05 10 00 10 00 12 08 0A 04 08 01 10 05 10 00 12 08 0A 04 08 02 10 0410 04 12 08 0A 04 08 03 10 03 10 00 12 08 0A 04 08 04 10 02 10 00 12 08 0A 04 08 05 10 01 10 00 12 08 0A 04 08 02 10 05 10 01 12 08 0A 04 08 03 10 04 10 00 12 08 0A 04 08 04 10 03 10 00 12 08 0A 04 08 05 10 02 10 00 12 08 0A 04 08 0310 05 10 00 12 08 0A 04 08 04 10 04 10 07 12 08 0A 04 08 05 10 03 10 00 12 08 0A 04 08 04 10 05 10 00 12 08 0A 04 08 05 10 04 10 00 12 08 0A 04 08 05 10 05 10 00 1A CD 01 0A 9F 01 08 FE B8 EC B7 C0 5A 10 A9 E6 BD 01 18 FF FF FF FF FF FF FFFF FF 01 22 85 01 68 74 74 70 73 3A 2F 2F 74 68 69 72 64 77 78 2E 71 6C 6F 67 6F 2E 63 6E 2F 6D 6D 6F 70 65 6E 2F 76 69 5F 33 32 2F 51 30 6A 34 54 77 47 54 66 54 49 4A 39 4B 79 69 63 74 38 33 64 38 51 79 69 63 4B 42 33 4E 70 48 37 50 31 3444 43 42 33 69 61 66 53 76 58 52 6D 4F 38 37 4A 61 56 41 37 61 63 66 70 44 32 65 74 57 31 68 30 31 50 6C 69 61 4B 6B 51 6B 4B 43 50 6B 73 6F 57 59 34 74 55 69 63 77 2F 31 33 32 18 B5 E5 08 20 93 4E 2A 0F E9 9B A8 E8 BD A9 E4 B8 B6 E9 85 92E9 85 92 30 14 38 D9 A6 E7 AE 13 40 AF A3 D6 9B A5 32 50 00 58 00 1A DE 01 0A A0 01 08 E2 83 86 B8 C0 5A 10 A9 E6 BD 01 18 FF FF FF FF FF FF FF FF FF 01 22 86 01 68 74 74 70 73 3A 2F 2F 74 68 69 72 64 77 78 2E 71 6C 6F 67 6F 2E 63 6E 2F 6D 6D 6F 70 65 6E 2F 76 69 5F 33 32 2F 62 76 49 6B 4D 73 55 42 6F 37 69 61 6B 53 4C 6B 69 61 42 69 63 44 4A 51 49 64 6B 78 6A 4E 58 58 62 57 55 5A57 7A 51 50 4D 55 65 41 57 53 70 6C 51 7A 30 69 62 58 4A 4D 54 4B 44 48 4B 4B 6B 69 61 6C 4C 38 6F 61 6D 57 33 6F 34 42 50 52 47 30 4A 6F 74 32 66 4D 6D 6E 42 69 62 51 2F 31 33 32 12 04 08 02 10 04 18 CD E5 08 20 94 4E 2A 12 E9 9B A8 E8 BDA9 E4 B8 B6 E7 9A AE E7 9A AE E5 8D 8E 30 13 38 E3 AA A6 8F 1A 40 C6 D9 F8 AA A5 32 48 01 50 87 83 80 AB A5 32 58 00 20 98 01 28 00 38 00 42 04 08 00 10 00 42 04 08 01 10 00 42 04 08 02 10 00 50 00 58 00
[DEBUG] [2024/10/04 10:33:26] [PRJ/websocket_apn/main_test.go]:[  206]:   	>>> 1 - code 08[8] len:0-_
[DEBUG] [2024/10/04 10:33:26] [PRJ/websocket_apn/main_test.go]:[  206]:   	>>> 2 - code 12[18] len:184-06 08 01 12 08 0A 04 08 00 10 00 10 00 12 08 0A 04 08 00 10 01 10 00 12 08 0A 04 08 01 10 00 10 00 12 08 0A 04 08 00 10 02 10 00 12 08 0A 04 08 01 10 01 10 07 12 08 0A04 08 02 10 00 10 00 12 08 0A 04 08 00 10 03 10 00 12 08 0A 04 08 01 10 02 10 00 12 08 0A 04 08 02 10 01 10 00 12 08 0A 04 08 03 10 00 10 00 12 08 0A 04 08 00 10 04 10 00 12 08 0A 04 08 01 10 03 10 00 12 08 0A 04 08 02 10 02 10 00 12 08 0A04 08 03 10 01 10 00 12 08 0A 04 08 04 10 00 10 00 12 08 0A 04 08 00 10 05 10 00 12 08 0A 04 08 01 10 04 10 07 12 08 0A 04 08 02 10 03 10 04 12_


*/
//
func ParaLongKuMapInfo(dataBytes []byte) D.LongKuMapInfo {
	var allInfo D.LongKuMapInfo
	if len(dataBytes) < 5 {
		return allInfo
	}
	data := BytesToHexStr(dataBytes)
	// 	人员信息								                                          1 ID		                            2 X           3 Y                                            4 名字             5 体力           6 妖力              7 时间          8 当前层数
	re0 := regexp.MustCompile(`1A(?:[0-9A-F]{2}){1,2}0A(?:[0-9A-F]{2}){1,2}08([0-9A-F]{12})10(?:[0-9A-F]{2})+?120408([0-9A-F]{2})10([0-9A-F]{2})18(?:[0-9A-F]{2})+?2A[0-9A-F]{2}((?:[0-9A-F]{2})+?)30([0-9A-F]{2})3A((?:[0-9A-F]{2})+?)40([0-9A-F]{12})48([0-9A-F]{2})50((?:[0-9A-F]{2}){1,6})58(0[0-9])`)
	res0 := re0.FindAllStringSubmatch(data, -1)
	if len(res0) == 0 {
		log.Errorf("解析数据失败-%s", data)
	}
	for _, regRes := range res0 {
		if len(regRes) > 1 {
			person := D.LongKuPersonInfo{
				ID:          regRes[1],                                           //人员ID
				X:           int(OneByteHexStrToByte(regRes[2])),                 //当前位置X坐标
				Y:           int(OneByteHexStrToByte(regRes[3])),                 //当前位置Y坐标
				Name:        HexDataStrToStr(regRes[4]),                          //名字
				Strength:    int(OneByteHexStrToByte(regRes[5])),                 //剩余体力
				CombatValue: ParaIntDataByHexStr(regRes[6]),                      //妖力
				Time:        ParaHexStrToTimeMs(regRes[7]).Add(24 * time.Minute), //下一次体力刷新时间
				Level:       int(OneByteHexStrToByte(regRes[8])),                 //当前层数
			}
			log.Debugf("%+v", person)
			allInfo.Person = append(allInfo.Person, person)
		}
	}
	// 	当前位置								                                                       1 阳玉     2 阴玉
	re1 := regexp.MustCompile(`20(?:[0-9A-F]{2}){1,2}28(?:[0-9A-F]{2})+?38(?:[0-9A-F]{2})+?50(0[01])58(0[01])`)
	res1 := re1.FindAllStringSubmatch(data, -1)
	if len(res1) == 0 {
		log.Errorf("解析数据失败-%s", data)
	}
	//log.Debugf("%#v", res1)
	if len(res1) > 0 && len(res1[0]) > 1 {
		if res1[0][1] == "01" {
			allInfo.YangYu = true
		}
		if res1[0][2] == "01" {
			allInfo.YinYu = true
		}
		log.Debugf("阴玉 %t  阳玉 %t", allInfo.YinYu, allInfo.YangYu)
	}
	// 	当前位置								        1 未领取的礼物id
	re2 := regexp.MustCompile(`12[0-9A-F]{2}08([0-9A-F]{2})12[0-9A-F]{2}(?:3[0-9D])+?1800`)
	res2 := re2.FindAllStringSubmatch(data, -1)
	if len(res2) == 0 {
		log.Errorf("解析数据失败-%s", data)
	}
	for _, regRes := range res2 {
		allInfo.Gift = append(allInfo.Gift, OneByteHexStrToByte(regRes[1]))
	}
	if len(allInfo.Gift) > 0 {
		allInfo.YangYu = true
		allInfo.YinYu = true
		sort.Slice(allInfo.Gift, func(i, j int) bool {
			return allInfo.Gift[i] < allInfo.Gift[j]
		})
		log.Debugf("%+v", allInfo.Gift)
		return allInfo
	}
	// 定义正则表达式								       1 X		      2 Y 		     3 类型
	re := regexp.MustCompile(`12[0-9A-F]{2}0A0408([0-9A-F]{2})10([0-9A-F]{2})10([0-9A-F]{2})(?:18([0-9A-F]{2}07)20)?`)
	res := re.FindAllStringSubmatch(data, -1)
	if len(res) == 0 {
		log.Errorf("解析数据失败-%s", data)
	}
	maxLen := 0
	for i := 0; i*i <= len(res); i++ {
		maxLen = i
	}
	allInfo.MapData = make([][]byte, maxLen)
	for i := range allInfo.MapData {
		allInfo.MapData[i] = make([]byte, maxLen)
	}
	// 打印结果
	for _, regRes := range res {
		//fmt.Printf("%#v\n%#v\n", resI[num], regRes)
		if len(regRes) > 1 {
			x := OneByteHexStrToByte(regRes[1])
			y := OneByteHexStrToByte(regRes[2])
			dType := OneByteHexStrToByte(regRes[3])
			if dType == 3 {
				//regRes[4]//怪物或者宝箱类型
				//startIdx := resI[num][0] / 2
				//dLen := int(dataBytes[startIdx+1])
				//fmt.Printf("(%d,%d) type:%s data:%d \n", x, y, HexToHexStr(dataBytes[startIdx+14:startIdx+2+dLen]), ParaIntData(dataBytes[startIdx+14:startIdx+2+dLen]))
			}
			allInfo.MapData[x][y] = dType
		}
	}
	printRotated(allInfo.MapData)
	return allInfo
}

/*
[DEBUG] [2024/10/05 12:47:22] [PRJ/websocket_apn/main_test.go]:[  195]:   ↓↓↓↓↓ game cmd[10824]-unkown - len:58
[DEBUG] [2024/10/05 12:47:22] [PRJ/websocket_apn/main_test.go]:[  200]:   PayloadBytes: 71 AB 00 00 00 3A 00 00 2A 48 00 00 02D4 08 94 30 6F 08 00 12 16 0A 08 0A 04 08 00 10 03 10 04 12 0A 31 30 35 30 36 36 3D 32 30 30 20 09 28 C4 C8 DE D7 A5 32 30 60 40 D5 01

*/
//龙窟秘境-体力信息更新
func ParaLongKuStrengthInfo(dataBytes []byte) D.LongKuStrengthInfo {
	var info D.LongKuStrengthInfo
	data := BytesToHexStr(dataBytes)
	// 	当前位置							1 剩余体力    2 体力刷新时间     3                4 排名
	re0 := regexp.MustCompile(`20([0-9A-F]{2})28([0-9A-F]{12})30((?:[0-9A-F]{2})+?)40((?:[0-9A-F]{2})+)`)
	res0 := re0.FindAllStringSubmatch(data, -1)
	if len(res0) == 0 {
		log.Errorf("解析数据失败-%s", data)
	}
	if len(res0) > 0 && len(res0[0]) > 0 {
		info.Strength = int(OneByteHexStrToByte(res0[0][1]))             //剩余体力
		info.Time = ParaHexStrToTimeMs(res0[0][2]).Add(24 * time.Minute) //下一次体力刷新时间
		info.Ranking = int(ParaIntDataByHexStr(res0[0][4]))              //当前排名
	}
	log.Infof("龙窟秘境-剩余体力:%2d 当前排名:%4d 下一次体力刷新时间%s", info.Strength, info.Ranking, info.Time.Format("15:04:05"))
	return info
}

//打印当前地图信息
func printRotated(matrix [][]byte) {
	rows := len(matrix)
	if rows == 0 {
		return
	}
	cols := len(matrix[0])
	// 计算每行的字符宽度，以便对齐
	width := 2 // 每个数字占用的宽度（包括空格）
	for i := 0; i < rows+cols-1; i++ {
		// 计算当前行的索引范围
		start := i - cols + 1
		if start <= 0 {
			start = 0
		}
		end := i + 1
		if end >= rows {
			end = rows
		}

		// 计算前导空格数
		leadingSpaces := (rows - (end - start)) * width / 2
		fmt.Print(strings.Repeat("  ", leadingSpaces))

		// 输出当前行，注意这里是反向输出
		for j := end - 1; j >= start; j-- {
			// 计算当前列索引
			colIndex := i - j
			if colIndex >= 0 && colIndex < cols {
				fmt.Printf("%2d", matrix[j][colIndex])
				if j > start {
					fmt.Print("  ")
				}
			}
		}
		fmt.Println("")
	}
}
